PHP: Control Structures
Introduction to PHP Control Structures
Control structures are the backbone of programming logic in PHP. They allow you to control the flow of your program's execution based on different conditions. PHP supports several types of control structures, including conditional statements and loops.
If Statement
The if statement allows you to execute code blocks based on conditions.
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
}
?>
If-Else Statement
The if-else statement allows you to execute one block of code if a condition is true and another if it's false.
<?php
$age = 15;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
If-Elseif-Else Statement
The if-elseif-else statement allows you to test multiple conditions.
<?php
$score = 75;
if ($score >= 90) {
echo "A";
} elseif ($score >= 80) {
echo "B";
} elseif ($score >= 70) {
echo "C";
} else {
echo "F";
}
?>
Switch Statement
The switch statement is used to perform different actions based on different conditions.
<?php
$day = "Monday";
switch ($day) {
case "Monday":
echo "It's the start of the work week.";
break;
case "Friday":
echo "TGIF!";
break;
default:
echo "It's a regular day.";
}
?>
While Loop
The while loop executes a block of code as long as the specified condition is true.
<?php
$i = 1;
while ($i <= 5) {
echo $i . " ";
$i++;
}
// Outputs: 1 2 3 4 5
?>
Do-While Loop
The do-while loop will always execute the block of code once, then check the condition.
<?php
$i = 1;
do {
echo $i . " ";
$i++;
} while ($i <= 5);
// Outputs: 1 2 3 4 5
?>
For Loop
The for loop is used when you know in advance how many times you want to execute a statement or a block of statements.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
// Outputs: 1 2 3 4 5
?>
Foreach Loop
The foreach loop works only on arrays and objects, and is used to loop through each key/value pair in an array.
<?php
$colors = ["red", "green", "blue"];
foreach ($colors as $color) {
echo $color . " ";
}
// Outputs: red green blue
?>
Break Statement
The break statement is used to exit a loop prematurely.
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i . " ";
}
// Outputs: 1 2 3 4
?>
Continue Statement
The continue statement is used to skip the rest of the current loop iteration and continue with the next iteration.
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i . " ";
}
// Outputs: 1 2 4 5
?>
Match Expression (PHP 8.0+)
The match expression is similar to switch and provides a more concise syntax for conditional branching.
<?php
$status = 200;
$message = match($status) {
200, 300 => 'Success',
400 => 'Bad request',
500 => 'Server error',
default => 'Unknown status',
};
echo $message; // Outputs: Success
?>
Best Practices
- Use clear and descriptive condition statements.
- Avoid deep nesting of control structures to maintain readability.
- Consider using switch or match instead of multiple if-elseif statements when comparing a single variable against multiple values.
- Use foreach loops when working with arrays, as they're generally more readable and less prone to errors.
- Be cautious with infinite loops - always ensure there's a way to exit the loop.
Conclusion
Control structures are fundamental to creating dynamic and responsive PHP applications. They allow you to create complex logic flows, iterate through data, and make decisions based on various conditions. Understanding and effectively using these control structures is crucial for writing efficient and powerful PHP code.